package com.aster.service.manage.service.impl;

import com.aster.common.base.BaseServiceImpl;
import com.aster.common.dto.UserToken;
import com.aster.common.exception.ServiceException;
import com.aster.common.utils.PageUtils;
import com.aster.service.manage.dao.EnterpriseProfitRatioInfoDao;
import com.aster.service.manage.dao.EscortEnterpriseInfoDao;
import com.aster.service.manage.domain.TbEnterpriseProfitRatioInfo;
import com.aster.service.manage.domain.TbEscortEnterpriseInfo;
import com.aster.service.manage.dto.DtoEscortEnterpriseInfo;
import com.aster.service.manage.dto.do2dto.EscortEnterpriseInfoConvert;
import com.aster.service.manage.service.EscortEnterpriseInfoService;
import com.aster.service.manage.service.criteria.EscortEnterpriseInfoCriteria;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 
 * <br>
 * <b>功能：</b>TbEscortEnterpriseInfoService<br>
 */
@Service("escortEnterpriseInfoService")
public class EscortEnterpriseInfoServiceImpl  extends BaseServiceImpl implements EscortEnterpriseInfoService {
  private final static Logger log= Logger.getLogger(EscortEnterpriseInfoServiceImpl.class);
	

	@Autowired
    private EscortEnterpriseInfoDao dao;
	@Autowired
	private EnterpriseProfitRatioInfoDao profitRatioInfoDao;


		
	public EscortEnterpriseInfoDao getDao() {
		return dao;
	}


	@Override
	public PageUtils getEscortEnterpriseInfoForPage(EscortEnterpriseInfoCriteria criteria) {
		Integer count = dao.queryByCount(criteria);
		if(count == null || count == 0){
			return new PageUtils(null,0);
		}
		List<TbEscortEnterpriseInfo> list = dao.queryByList(criteria);
		List<DtoEscortEnterpriseInfo> dtoList = EscortEnterpriseInfoConvert.MAPPER.dos2dtos(list);
		List<DtoEscortEnterpriseInfo> dtoEscortEnterpriseInfos = new ArrayList<>();
		// 循环取出分润值
		for (DtoEscortEnterpriseInfo dtoEscortEnterpriseInfo:dtoList) {
			Integer enterpriseId = dtoEscortEnterpriseInfo.getId();
			TbEnterpriseProfitRatioInfo info = new TbEnterpriseProfitRatioInfo();
			info.setState(0);
			info.setEnterpriseId(enterpriseId);
			info = profitRatioInfoDao.queryByEnterpriseIdAndState(info);
			if (info != null){
				dtoEscortEnterpriseInfo.setProfitRetio(info.getProfitRetio());
			}
			dtoEscortEnterpriseInfos.add(dtoEscortEnterpriseInfo);
		}
		return new PageUtils(dtoEscortEnterpriseInfos,count);
	}

	@Override
	public void addEscortEnterpriseInfo(TbEscortEnterpriseInfo tbEscortEnterpriseInfo, UserToken userToken) throws ServiceException {
		if (tbEscortEnterpriseInfo == null){
			throw new ServiceException(10001,"参数为空");
		}
		try {
			tbEscortEnterpriseInfo.setCreateTime(new Date());
			tbEscortEnterpriseInfo.setUpdateTime(new Date());
			tbEscortEnterpriseInfo.setCreateUserId(Integer.parseInt(userToken.getUserId()));
			tbEscortEnterpriseInfo.setUpdateUserId(Integer.parseInt(userToken.getUserId()));
			tbEscortEnterpriseInfo.setState(1);
			tbEscortEnterpriseInfo.setDeleted(1);
			dao.add(tbEscortEnterpriseInfo);

		}catch (Exception ex){
			throw new ServiceException(10000,ex.getMessage());
		}
	}

	@Override
	public void editEscortEnterpriseInfo(TbEscortEnterpriseInfo tbEscortEnterpriseInfo, UserToken userToken) throws ServiceException {
		if (tbEscortEnterpriseInfo == null){
			throw new ServiceException(10001,"参数为空");
		}
		try{
			tbEscortEnterpriseInfo.setUpdateUserId(Integer.parseInt(userToken.getUserId()));
			tbEscortEnterpriseInfo.setUpdateTime(new Date());
			dao.updateBySelective(tbEscortEnterpriseInfo);
		}catch (Exception ex){
			ex.printStackTrace();
			throw new ServiceException(10000,ex.getMessage());
		}
	}
}
